package com.gjun.struts.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.gjun.struts.bean.BookJsonBean;
import com.gjun.struts.bean.OrderJsonBean;
import com.gjun.struts.bean.ext.Order;
import com.gjun.struts.resource.dao.IOrderDao;
import com.gjun.struts.util.JSONHelper;

public class MgrOrdersAction {
	
	private Date starttimer;
	
	private Date endtimer;
	
	private int pageSize = 10; 
    
    private int totalSize;
    
    private int totalPage;
    
    private int curPage;
    
    private int id;
    
    private int[] delId;
    
    private String receiveName;
    
    private double totalPrice;
    
    private String email; 
    
    private String result;
    
    private IOrderDao orderDao;
    
    /**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}

	/**
	 * @param result the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}

	/**
	 * @return the orderDao
	 */
	public IOrderDao getOrderDao() {
		return orderDao;
	}

	/**
	 * @param orderDao the orderDao to set
	 */
	public void setOrderDao(IOrderDao orderDao) {
		this.orderDao = orderDao;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the receiveName
	 */
	public String getReceiveName() {
		return receiveName;
	}

	/**
	 * @param receiveName the receiveName to set
	 */
	public void setReceiveName(String receiveName) {
		this.receiveName = receiveName;
	}

	/**
	 * @return the totalPrice
	 */
	public double getTotalPrice() {
		return totalPrice;
	}

	/**
	 * @param totalPrice the totalPrice to set
	 */
	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}

	/**
	 * @return the starttimer
	 */
	public Date getStarttimer() {
		return starttimer;
	}

	/**
	 * @param starttimer the starttimer to set
	 */
	public void setStarttimer(Date starttimer) {
		this.starttimer = starttimer;
	}

	/**
	 * @return the endtimer
	 */
	public Date getEndtimer() {
		return endtimer;
	}

	/**
	 * @param endtimer the endtimer to set
	 */
	public void setEndtimer(Date endtimer) {
		this.endtimer = endtimer;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the totalSize
	 */
	public int getTotalSize() {
		return totalSize;
	}

	/**
	 * @param totalSize the totalSize to set
	 */
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	/**
	 * @return the totalPage
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * @param totalPage the totalPage to set
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	/**
	 * @return the curPage
	 */
	public int getCurPage() {
		return curPage;
	}

	/**
	 * @param curPage the curPage to set
	 */
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the delId
	 */
	public int[] getDelId() {
		return delId;
	}

	/**
	 * @param delId the delId to set
	 */
	public void setDelId(int[] delId) {
		this.delId = delId;
	}
    
    
	
	public String loadOrdersData(){
		String conditions = null;
		if(!receiveName.isEmpty()){
			conditions += "o.receiveName ='"+receiveName +"' ";
		}
		if(totalPrice != 0){
			conditions += "o.totalPrice ='"+totalPrice +"' ";
		}
		List<Order> orders = orderDao.getOrder(conditions, email); 
		this.totalSize = orders.size();
		totalPage = (totalSize + pageSize - 1) /pageSize;
		if(curPage == 0)
			curPage = 1;
		int start = (curPage - 1)*pageSize;
		List<Order> orders2 = orderDao.getOrder(conditions, email,start,pageSize);
		List<OrderJsonBean> beans = new ArrayList();
		for (Order order : orders2) {
			OrderJsonBean bean = new OrderJsonBean(order);
			beans.add(bean);
		}
		result = JSONHelper.orderToJson(beans);
		
		return "success"; 
	}
	
	public String loadOrdersEditById(){
		Order order = orderDao.getOrderById(id);
		OrderJsonBean bean = new OrderJsonBean(order);
		ServletActionContext.getRequest().getSession().setAttribute("editOrderBean", bean);
		result = "edit_to_next_page";
		return "success";
	}
	
	public String toDetails(){
		result = "edit_to_next_page";
		ServletActionContext.getRequest().getSession().setAttribute("orderId", id);
		return "success";
	}
}
